﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AES加密与解密
{
    class Program
    {
        static void Main(string[] args)
        {
            const string key = "1234567887654321";
            string str = "\\n";
            Console.WriteLine("待加密内容：" + str);
            string resultStr = AESHelper.AESEncrypt2(str, key);
            Console.WriteLine("加密结果为：" + resultStr);

            string resultStr2 = AESHelper.AESDecrypt2(resultStr, key);
            Console.WriteLine("解密结果为：" + resultStr2);

            string strCbc = AESHelper.AESEncrypt2CBC("12345", "123qweasdzxc4567", "AKLSDJHFTYEIOVNB");
            byte[] enArr = Convert.FromBase64String(strCbc);
            string str2 = "";
            foreach (var b in enArr)
            {
                str2 += b.ToString("X2") + " ";
            }
            Console.WriteLine("CBC加密结果:\r\n" + str2);

            string resultStr3 = AESHelper.AESDecrypt2CBC(strCbc, "123qweasdzxc4567", "AKLSDJHFTYEIOVNB");

            Console.WriteLine("CBC解密结果:" + resultStr3);

            Console.ReadLine();
        }


        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="str">需要加密的字符串</param>
        /// <param name="key">32位密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string AESEncrypt2(string str, string key)
        {
            Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
            //Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(str);
            Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(str);
            var rijndael = new System.Security.Cryptography.RijndaelManaged();
            rijndael.Key = keyArray;
            rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
            rijndael.Padding = System.Security.Cryptography.PaddingMode.Zeros;
            //rijndael.KeySize = 128;
            //rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
            System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="str">需要解密的字符串</param>
        /// <param name="key">32位密钥</param>
        /// <returns>解密后的字符串</returns>
        public static string AESDecrypt2(string str, string key)
        {
            Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
            Byte[] toEncryptArray = Convert.FromBase64String(str);
            var rijndael = new System.Security.Cryptography.RijndaelManaged();
            rijndael.Key = keyArray;
            rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
            rijndael.Padding = System.Security.Cryptography.PaddingMode.Zeros;
            //rijndael.KeySize = 128;
            //rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
            System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            //return System.Text.Encoding.UTF8.GetString(resultArray);
            return System.Text.Encoding.UTF8.GetString(resultArray);
        }
    }
}
